package com.yunhai.device.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yunhai.common.dto.GlobalException;
import com.yunhai.common.dto.PaginationVo;
import com.yunhai.common.utils.ObjectUtil;
import com.yunhai.common.utils.ReflectionCopy;
import com.yunhai.device.mapper.FdsBusinessStartRulesetMapper;
import com.yunhai.device.service.FdsBusinessStartRulesetService;
import com.yunhai.model.entity.fds.FdsBusinessStartRulesetEntity;
import com.yunhai.model.vo.fds.FdsBusinessStartRulesetQuery;
import com.yunhai.model.vo.fds.FdsBusinessStartRulesetVo;
import org.apache.commons.beanutils.PropertyUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class FdsBusinessStartRulesetServiceImpl implements FdsBusinessStartRulesetService {

    @Resource
    private FdsBusinessStartRulesetMapper fdsBusinessStartRulesetMapper;

    /**
     * 获取 FdsBusinessStartRuleset 分页列表
     */
    @Transactional
    public IPage getFdsBusinessStartRulesetPagination(PaginationVo paginationVo, FdsBusinessStartRulesetQuery fdsBusinessStartRulesetQuery ) throws Exception {

        IPage page = new Page( paginationVo.getPageIndex(), paginationVo.getPageSize() );
        LambdaQueryWrapper<FdsBusinessStartRulesetEntity> lqs = new LambdaQueryWrapper();
        fdsBusinessStartRulesetMapper.selectPage(page, lqs);

        List<FdsBusinessStartRulesetEntity> record = page.getRecords();
        if( !ObjectUtil.isNotEmptyList(record)){
            return page;
        }
        return page;
    }


    /**
     * 获取 FdsBusinessStartRuleset 所有数据
     *
     */
    @Transactional
    public List<FdsBusinessStartRulesetEntity> getFdsBusinessStartRulesetAllList( FdsBusinessStartRulesetQuery fdsBusinessStartRulesetQuery ) throws Exception {

        LambdaQueryWrapper<FdsBusinessStartRulesetEntity> lqs = new LambdaQueryWrapper();
        List<FdsBusinessStartRulesetEntity> list = fdsBusinessStartRulesetMapper.selectList(lqs);
        return list;
    }


    /**
     * 获取 FdsBusinessStartRuleset 详情
     */
    @Transactional
    public FdsBusinessStartRulesetEntity getFdsBusinessStartRulesetDetail( Long id) throws Exception {

        FdsBusinessStartRulesetEntity fdsBusinessStartRulesetEntity = fdsBusinessStartRulesetMapper.selectById(id);
        return fdsBusinessStartRulesetEntity;
    }


    /**
     * 新增 FdsBusinessStartRuleset
     * */
    @Transactional
    public void addFdsBusinessStartRuleset(FdsBusinessStartRulesetVo fdsBusinessStartRulesetVo) throws Exception {

        FdsBusinessStartRulesetEntity fdsBusinessStartRulesetEntity = new FdsBusinessStartRulesetEntity();
        ReflectionCopy.copyProperties( fdsBusinessStartRulesetEntity, fdsBusinessStartRulesetVo );
        fdsBusinessStartRulesetMapper.insert(fdsBusinessStartRulesetEntity);
        return;
    }


    /**
     * 更新 FdsBusinessStartRuleset
     * */
    @Transactional
    public void updateFdsBusinessStartRuleset( FdsBusinessStartRulesetVo fdsBusinessStartRulesetVo) throws Exception {

        if( fdsBusinessStartRulesetVo.getId() == null ){
            throw GlobalException.gen(10021, "id不存在");
        }
        FdsBusinessStartRulesetEntity fdsBusinessStartRulesetEntity = new FdsBusinessStartRulesetEntity();
        ReflectionCopy.copyProperties( fdsBusinessStartRulesetEntity, fdsBusinessStartRulesetVo );
        fdsBusinessStartRulesetMapper.updateById(fdsBusinessStartRulesetEntity);
        return;
    }

    /**
     * 删除 FdsBusinessStartRuleset
     * */
    @Transactional
    public void batchRemoveFdsBusinessStartRuleset( List<Long> ids ) throws Exception {

        if( !ObjectUtil.isNotEmptyList(ids) ){
            throw GlobalException.gen(10011, "要删除的id不存在");
        }
        fdsBusinessStartRulesetMapper.deleteBatchIds(ids);
    }

}